/**
 * Created by IntelliJ IDEA.
 * User: holtnick
 * Date: 24/05/12
 * Time: 15:14
 * To change this template use File | Settings | File Templates.
 */
package com.tn.openrpc
{
	import com.tn.openrpc.channel.IRemoteChannel;
	import com.tn.openrpc.channel.RemoteChannelRequest;
	import mx.rpc.AsyncToken;

	public class RemoteChannelBasedRemoteMethod implements IRemoteMethod
	{

		/*============================================================================*/
		/* Private Properties                                                         */
		/*============================================================================*/

		private var methodName:String;

		private var remoteChannel:IRemoteChannel;

		private var target:String;

		/*============================================================================*/
		/* Constructor                                                                */
		/*============================================================================*/

		public function RemoteChannelBasedRemoteMethod(
			remoteChannel:IRemoteChannel,
			target:String,
			methodName:String)
		{
			this.remoteChannel = remoteChannel;
			this.target = target;
			this.methodName = methodName;
		}


		/*============================================================================*/
		/* Public Functions                                                           */
		/*============================================================================*/

		/**
		 * {@inheritDoc}
		 */
		public function invoke(... args):AsyncToken
		{
			return remoteChannel.send(retrieveRemoteChannelRequest(args));
		}

		/*============================================================================*/
		/* Protected Functions                                                        */
		/*============================================================================*/

		protected function retrieveRemoteChannelRequest(args:Array):RemoteChannelRequest
		{
			var remoteChannelRequest:RemoteChannelRequest = new RemoteChannelRequest();
			remoteChannelRequest.target = target;
			remoteChannelRequest.methodName = methodName;
			remoteChannelRequest.args = args;

			return remoteChannelRequest;
		}
	}
}
